<template>
  <div id="chartunit" class="full-width full-height"></div>
</template>
<script>
import * as echarts from "echarts";
import { addResizeListener, removeResizeListener } from "@/utils/resize";
import { chartColor } from "@/utils/chartColor";
export default {
  data() {
    return {
      resizeListener: null,
      chart: null
    };
  },
  props: {
    xData: {
      type: Array,
      default: () => {
        return [];
      }
    },
    yData: {
      type: Array,
      default: () => {
        return [];
      }
    },
    total: {
      type: Number,
      default: 0
    }
  },
  methods: {
    addResizeListener() {
      this.resizeListener = addResizeListener(this.$el, () => {
        this.resize();
      });
    },
    resize() {
      if (this.chart) {
        this.chart.resize();
      }
    },
    initChart() {
      let ranking1 = ``;
      let ranking2 = ``;
      let ranking3 = ``;
      let ranking4 = `${document.location.protocol}//${window.location.hostname}:${window.location.port}/fabu-admin/uiImgs/icon-build.png`;
      let chartIcon = ``;

      let xdata = {
        value: this.xData
      };
      let sdata = {
        value: this.yData
      };
      let rankings = [
        ranking1,
        ranking2,
        ranking3,
        ranking4,
        ranking4,
        ranking4,
        ranking4,
        ranking4,
        ranking4,
        ranking4,
        ranking4,
        ranking4,
        ranking4,
        ranking4,
        ranking4,
        ranking4,
        ranking4
      ];
      let rich_images = {};
      xdata.value.map((v, k) => {
        let rich_images_key = `img${k}`;
        let rich_images_item = {
          width: k > 2 ? 44 : 34,
          height: k > 2 ? 44 : 24,
          text: 2,
          backgroundColor: {
            image:
              k == 0
                ? ranking1
                : k == 1
                ? ranking2
                : k == 2
                ? ranking3
                : ranking4
          }
        };
        if (k < 3) {
          rich_images_item.width = 34;
          rich_images_item.height = 24;
        } else {
          rich_images_item.width = 34;
          rich_images_item.height = 40;
        }
        rich_images[rich_images_key] = rich_images_item;
      });

      let dataZoomMove = {
        start: 0,
        end: 4
      };
      let option = {
        // backgroundColor: "#2B2E33",
        title: {
          text: "",
          textAlign: "center",
          left: "50%",
          textStyle: {
            color: chartColor().title,
            fontSize: 16,
            lineHeight: 25
          }
        },
        tooltip: {
          trigger: "axis",
          backgroundColor: "#081429",
          textStyle: {
            color: "#fff"
          },
          borderColor: "rgba(255,255,255,.2)",
          axisPointer: {
            lineStyle: {
              color: "rgba(255,255,255,.1)"
            }
          },
          formatter: fp => {
            return `${fp[0].name}:${fp[0].value} \n占比：${(
              (fp[0].value / this.total) *
              100
            ).toFixed(2)}%`;
          },
          extraCssText: "white-space:pre-wrap"
        },
        dataZoom: [
          {
            show: true, // 为true 滚动条出现
            realtime: true,
            type: "slider", // 有type这个属性，滚动条在最下面，也可以不行，写y：36，这表示距离顶端36px，一般就是在图上面。
            height: "100%", // 表示滚动条的高度，也就是粗细
            startValue: dataZoomMove.start, // 表示默认展示20%～80%这一段。
            endValue: dataZoomMove.end,
            width: 6,
            right: "20",
            top: "6%", //位置和grid配置注意下
            height: "86%",
            yAxisIndex: [0, 1], //关联多个y轴
            moveHandleStyle: {
              color: "rgba(89, 202, 241,.5)"
            },
            moveHandleSize: "6",
            emphasis: {
              moveHandleStyle: {
                color: "rgba(89, 202, 241,.5)"
              }
            },
            textStyle: {
              color: chartColor().drak
            },
            backgroundColor: "rgba(255,255,255,.1)",
            borderColor: "rgba(255,255,255,0)",
            fillerColor: "rgba(0,0,0,0)",
            handleSize: "6",
            handleStyle: {
              color: "rgba(255,255,255,0)"
            },
            brushStyle: {
              color: "rgba(129, 243, 253)"
            }
          },
          {
            //没有下面这块的话，只能拖动滚动条，鼠标滚轮在区域内不能控制外部滚动条
            type: "inside",
            yAxisIndex: 0,
            zoomOnMouseWheel: false, //滚轮是否触发缩放
            moveOnMouseMove: true, //鼠标滚轮触发滚动
            moveOnMouseWheel: true
          }
        ],
        grid: {
          containLabel: true,
          // 边距自行修改
          bottom: "0",
          left: "1%",
          top: "10",
          right: "0"
        },
        xAxis: {
          type: "value",
          axisLabel: {
            show: false
          },
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          splitLine: {
            show: false
          }
        },
        yAxis: [
          {
            type: "category",
            data: xdata.value,
            inverse: true,
            axisLabel: {
              fontSize: "18px",
              inside: true,
              verticalAlign: "bottom",
              lineHeight: 54,
              margin: 0, //刻度标签与轴线之间的距离
              formatter: function(value) {
                let k = xdata.value.indexOf(value);
                let index = k;
                if (index > 2) {
                  return `{z|}{img${index}|}{z|} {a|${value}}`;
                }
                return `{img${index}|} {a|${value}}`;
              },
              rich: {
                a: {
                  fontSize: 16,
                  color: chartColor().drak,
                  padding: [4, 0, 0, 0]
                },

                z: {
                  width: 6,
                  height: 6
                },
                ...rich_images
              }
            },
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            splitLine: {
              show: false,
              lineStyle: {
                color: "#13387a"
              }
            }
          },

          {
            type: "category",
            data: xdata.value,
            inverse: true,
            axisLabel: {
              inside: true,
              verticalAlign: "bottom",
              lineHeight: 54,
              margin: 0, //刻度标签与轴线之间的距离
              formatter: function(value) {
                let k = xdata.value.indexOf(value);
                let index = k;
                return `{a|${sdata.value[index]}}{u|条}`;
              },
              rich: {
                a: {
                  fontSize: 22,
                  color: "#e85011",
                  padding: [4, 0, 0, 0]
                },
                u: {
                  color: "#e85011",
                  fontSize: 16,
                  padding: [2, 0, 0, 4]
                }
              }
            },
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            splitLine: {
              show: false,
              lineStyle: {
                color: "#13387a"
              }
            }
          }
        ],
        series: [
          {
            data: sdata.value,
            type: "bar",
            barWidth: 8,
            showBackground: true,
            backgroundStyle: {
              color: "#e4effe"
            },
            itemStyle: {
              borderRadius: 40,
              normal: {
                color: function(params) {
                  // 给出颜色组
                  var colorList = ["#f0520b", "#f3a918", "#559bf3"];
                  return params.dataIndex == 0
                    ? colorList[0]
                    : params.dataIndex == 1 || params.dataIndex == 2
                    ? colorList[1]
                    : colorList[2];
                },
                global: false // 缺省为 false
              }
            },
            label: {
              show: false,
              position: "insideRight",
              distance: -15,
              formatter: () => {
                return `{z|}{a|}`;
              }
              //   rich: {
              //     a: {
              //       widht: 30,
              //       height: 30,
              //       backgroundColor: {
              //         // image: chartIcon
              //       }
              //     }
              //   }
            }
          }
        ]
      };
      this.chart = echarts.init(document.getElementById("chartunit"));
      this.chart.setOption(option, true);
      // 自动轮播和鼠标移入移出的停止和开启
      let dataZoomMoveTimer = null;
      const startMoveDataZoom = (chart, dataZoomMove) => {
        dataZoomMoveTimer = setInterval(() => {
          dataZoomMove.start += 1;
          dataZoomMove.end += 1;
          if (dataZoomMove.end > sdata.value.length - 1) {
            dataZoomMove.start = 0;
            dataZoomMove.end = 4;
          }
          chart.setOption({
            dataZoom: [
              {
                type: "slider", // 有type这个属性，滚动条在最下面，也可以不行，写y：36，这表示距离顶端36px，一般就是在图上面。
                startValue: dataZoomMove.start,
                endValue: dataZoomMove.end
              }
            ]
          });
        }, 5000);
      };
      startMoveDataZoom(this.chart, dataZoomMove);
      let chartDom = this.chart.getDom();
      chartDom.addEventListener("mouseout", () => {
        if (dataZoomMoveTimer) return;
        let dataZoomMove_get = this.chart.getOption().dataZoom[0];
        dataZoomMove.start = dataZoomMove_get.startValue;
        dataZoomMove.end = dataZoomMove_get.endValue;
        startMoveDataZoom(this.chart, dataZoomMove);
      });
      // 移入
      // this.chart.on
      chartDom.addEventListener("mouseover", () => {
        clearInterval(dataZoomMoveTimer);
        dataZoomMoveTimer = undefined;
      });
    }
  },
  mounted() {
    this.addResizeListener();
    this.initChart();
  },
  beforeDestroy() {
    removeResizeListener(this.resizeListener);
  }
};
</script>
